package com.haohan.jingyuan.customer.helper;

import com.haohan.jingyuan.customer.dto.resp.CustomerInfoResp;
import com.haohan.jingyuan.customer.dto.resp.TagItem;
import com.haohan.jingyuan.customer.entity.CustomerGroupInfo;
import com.haohan.jingyuan.customer.entity.CustomerInfo;
import com.haohan.jingyuan.customer.entity.CustomerTag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 客户信息响应对象转换工具类
 *
 * @ClassName CustomerInfoRespHelper
 * @Description 客户信息实体与响应对象转换工具
 * @Author hexijun
 * @Time 2025/01/19 10:00
 * @Version 1.0
 */
public class CustomerInfoRespHelper {

    /**
     * 将CustomerInfo实体转换为CustomerInfoResp响应对象
     *
     * @param customerInfo 客户信息实体
     * @return 客户信息响应对象
     */
    public static CustomerInfoResp convert2CustomerInfoResp(CustomerInfo customerInfo) {
        return convert2CustomerInfoResp(customerInfo, null, null, null);
    }

    /**
     * 将CustomerInfo实体转换为CustomerInfoResp响应对象（带字典、分组、标签信息）
     *
     * @param customerInfo 客户信息实体
     * @param industryMap 行业字典Map，key为dictKey，value为dictValue
     * @param groupMap 分组信息Map，key为groupId，value为CustomerGroupInfo
     * @param tagMap 标签信息Map，key为tagCode，value为CustomerTag
     * @return 客户信息响应对象
     */
    public static CustomerInfoResp convert2CustomerInfoResp(CustomerInfo customerInfo,
                                                             Map<String, String> industryMap,
                                                             Map<String, CustomerGroupInfo> groupMap,
                                                             Map<String, CustomerTag> tagMap) {
        if (customerInfo == null) {
            return null;
        }

        CustomerInfoResp customerInfoResp = new CustomerInfoResp();
        
        // 复制基础字段
        customerInfoResp.setId(customerInfo.getId());
        customerInfoResp.setCustomerId(customerInfo.getCustomerId());
        customerInfoResp.setName(customerInfo.getName());
        customerInfoResp.setIndustry(customerInfo.getIndustry());
        
        // 设置行业名称
        if (customerInfo.getIndustry() != null && industryMap != null) {
            String industryName = industryMap.get(customerInfo.getIndustry());
            if (industryName != null) {
                customerInfoResp.setIndustryName(industryName);
            }
        }
        
        customerInfoResp.setOwner(customerInfo.getOwner());
        customerInfoResp.setCategory(customerInfo.getCategory());
        
        // 设置分组名称
        if (customerInfo.getCategory() != null && groupMap != null) {
            CustomerGroupInfo groupInfo = groupMap.get(customerInfo.getCategory());
            if (groupInfo != null) {
                customerInfoResp.setCategoryName(groupInfo.getGroupName());
            }
        }
        
        // 设置标签列表
        if (customerInfo.getTags() != null && !customerInfo.getTags().trim().isEmpty() && tagMap != null) {
            List<TagItem> tagList = Stream.of(customerInfo.getTags().split(","))
                    .map(String::trim)
                    .filter(code -> !code.isEmpty())
                    .map(code -> {
                        CustomerTag tagInfo = tagMap.get(code);
                        if (tagInfo != null) {
                            TagItem tagItem = new TagItem();
                            tagItem.setCode(tagInfo.getTagCode());
                            tagItem.setValue(tagInfo.getTagName());
                            return tagItem;
                        }
                        return null;
                    })
                    .filter(item -> item != null)
                    .collect(Collectors.toList());
            customerInfoResp.setTags(tagList);
        } else {
            customerInfoResp.setTags(new ArrayList<>());
        }
        
        customerInfoResp.setContactName(customerInfo.getContactName());
        customerInfoResp.setContactPhone(customerInfo.getContactPhone());
        customerInfoResp.setRemarks(customerInfo.getRemarks());
        customerInfoResp.setCreator(customerInfo.getCreator());
        customerInfoResp.setGmtCreated(customerInfo.getGmtCreated());
        customerInfoResp.setModifier(customerInfo.getModifier());
        customerInfoResp.setGmtModified(customerInfo.getGmtModified());
        customerInfoResp.setIsDeleted(customerInfo.getIsDeleted());

        return customerInfoResp;
    }
}
